package com.lintcode.solution;

public class Solution_13 {
    public static void main(String[] args) {
        Solution_13 solution = new Solution_13();
//        System.out.println(solution.strStr("source", "target"));
//        System.out.println(solution.strStr("abcdabcdefg", "bcd"));
        System.out.println(solution.strStr("lintcode", "lintcode"));
//        System.out.println(solution.strStr("source", "rced"));
//        System.out.println(solution.strStr("tartarget", "target"));
    }

    public int strStr(String source, String target) {
        if (("".equals(source) && "".equals(target)) || (!"".equals(source) && "".equals(target))) {
            return 0;
        } else if (((source == null || "".equals(source)) && !"".equals(target)) || target == null) {
            return -1;
        }
        char[] chars_source = source.toCharArray();
        char[] chars_target = target.toCharArray();
        for (int i = 0; i < chars_source.length; i++) {
            if (chars_source[i] != chars_target[0]) {
                continue;
            }
            if ((chars_source.length - i) >= chars_target.length) {
                int index = i;
                for (int j = 0; j < chars_target.length; j++, index++) {
                    char a = chars_target[j];
                    char b = chars_source[index];
                    if (a != b) {
                        break;
                    }
                }
                if (index - i == chars_target.length) {
                    return i;
                }
            } else {
                break;
            }

        }
        return -1;
    }
}
